<?php
/**
 * Created by PhpStorm.
 * User: tangchao
 * Date: 19-3-18
 * Time: 上午9:52
 */

namespace App\Lib;

/**
 * 反射机制调用类
 * Class ClassArr
 * @package App\Lib
 */
class ClassArr
{
    /**
     * 上传启动类
     * @return array
     */
    public function uploadClassStart()
    {
        return [
            'image' => "App\Lib\Upload\Image",
            'video' => "App\Lib\Upload\Video",
        ];
    }

    /**
     * 利用反射机制调用具体类库
     * @param $type 类库子类
     * @param $supportedClass   支持类库
     * @param array $params 初始化参数
     * @param bool $needInstance 实例化标识
     * @return bool|object
     * @throws \ReflectionException
     */
    public function initClass($type, $supportedClass, $params=[], $needInstance = true)
    {
        if (!array_key_exists($type, $supportedClass)){
            return false;
        }

        $className = $supportedClass[$type];

        return $needInstance ? (new \ReflectionClass($className))->newInstanceArgs($params) : $className;
    }
}